import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_basic_library/flutter_basic_library.dart';
import 'package:flutter_basic_library/widget/choice_chip_select_widget.dart';


showDoubleLevelBottomSheet(BuildContext context,List<BaseSelectEntity> data,
    {SureSeclectCall? seclectCall,BaseSelectEntity? firstSelect,BaseSelectEntity? secondSelect}){
  //用于在底部打开弹框的效果
  showModalBottomSheet(
      isScrollControlled: true,
      builder: (BuildContext context) {
        //构建弹框中的内容
        return TwoLevelRelationWidget(data: data,seclectCall:seclectCall,
        firstSelect: firstSelect,secondSelect: secondSelect,);
      },
      backgroundColor: Colors.transparent,//重要
      context: context);
}

typedef SureSeclectCall = void Function(BaseSelectEntity firstSelect, BaseSelectEntity secondSelect);

/**
 * by zjh
 * 2022/12/9  14:45
 * 2级的关联选择器
 */
class TwoLevelRelationWidget extends StatefulWidget{

  List<BaseSelectEntity>? data;

  BaseSelectEntity? firstSelect;

  BaseSelectEntity? secondSelect;

  SureSeclectCall? seclectCall;

  TwoLevelRelationWidget({Key? key,this.data,this.firstSelect,this.secondSelect,this.seclectCall}) : super(key: key);

  @override
  State<StatefulWidget> createState() =>TwoLevelRelationWidgetState();

}

class TwoLevelRelationWidgetState extends State<TwoLevelRelationWidget>{

  BaseSelectEntity? firstSelect;

  BaseSelectEntity? secondSelect;

  int firstIndex = 0 ;
  int secondIndex = 0;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if(widget.firstSelect==null){
      firstIndex = 0;
      firstSelect = widget.data![firstIndex];
    }else{
      firstSelect = widget.firstSelect;
      firstIndex = widget.data!.lastIndexWhere((element) => firstSelect!.getKey().compareTo(element.getKey())==0);
    }
    if(widget.secondSelect==null){
      secondIndex = 0;
      secondSelect = widget.data![firstIndex].getList()[secondIndex];
    }else{
      secondSelect = widget.secondSelect;
      secondIndex = widget.data![firstIndex].getList().lastIndexWhere((element) => secondSelect!.getKey().compareTo(element.getKey())==0);
    }
  }

  sureData(){
    widget.seclectCall!.call(firstSelect!,secondSelect!);
    NavigatorUtil.goBack(context);
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      decoration: BoxDecoration(color: Colors.white,borderRadius: BorderRadius.only(topLeft: Radius.circular(10.r),topRight: Radius.circular(10.r) )),
      padding: EdgeInsets.all(15.w),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              GestureDetector(child: Text('取消',style:  TextStyle(fontSize: 16.sp),),onTap: ()=> NavigatorUtil.goBack(context),),
              GestureDetector(child: Text('确认',style: TextStyle(fontSize: 16.sp,color: Colors.blueAccent),),onTap: ()=>sureData(),),
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              getData(widget.data!,0),
              getData(widget.data![firstIndex].getList(),1),
            ],
          ),
        ],
      ),
    );
  }

  Widget getData(List<BaseSelectEntity> data,int type){
    return Expanded(
      child:Container(
        height: 180.w,
        child: CupertinoPicker(
          scrollController: FixedExtentScrollController(initialItem:type==0?firstIndex:secondIndex),///设置默认值
          // diameterRatio: 1.5,///直径比，double类型
          // offAxisFraction: 0.2, //轴偏离系数  轴偏移，默认是0.0。控制选中的子widget的左右偏移
          // useMagnifier: true, //使用放大镜 默认false。
          // magnification: 1.5, //当前选中item放大倍数useMagnifier为true才管用
          // squeeze:0.7,///压缩，这个控制的children之间的空隙，和diameterRatio的效果有相似之处。
          itemExtent: 32, //行高
          // backgroundColor: Colors.white, ///选中器背景色
          selectionOverlay:Container(decoration:const BoxDecoration(color: Colors.transparent),),
          onSelectedItemChanged:(value){
             getItemValue(value,type);
          },
          children: getItemData(data),
        ),
      ),
    );
  }

  getItemValue(int index,int type){

    if(type==0){
      firstIndex = index;
      firstSelect = widget.data![index];
      secondIndex = 0;
      secondSelect = widget.data![firstIndex].getList()[secondIndex];
      setState(() {

      });
    }else{
      secondIndex = index;
      secondSelect = widget.data![firstIndex].getList()[secondIndex];
    }
  }

  List<Widget> getItemData(List<BaseSelectEntity> data){
    List<Widget> widgets =[];
    data.forEach((element) {
      widgets.add(Container(
        margin:  EdgeInsets.only(top: 3.w,bottom: 3.w),
        child: Text(element.getTag,style: TextStyle(fontSize: 16.sp),),
      ));
    });
    return widgets;
  }

}